package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.recycle.domain.IntegralCommodity;
import com.ruoyi.recycle.domain.IntegralCommoditySize;
import com.ruoyi.recycle.dto.IntegralCommodityRepertory;
import com.ruoyi.recycle.mapper.CommodityRepertoryMapper;
import com.ruoyi.recycle.mapper.IntegralCommodityMapper;
import com.ruoyi.recycle.mapper.IntegralCommoditySizeMapper;
import com.ruoyi.recycle.mapper.IntegralOrderMapper;
import com.ruoyi.recycle.service.IIntegralCommoditySizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 积分商品尺寸Service业务层处理
 *
 * @author Wubk
 * @date 2022-05-22
 */
@Service
public class IntegralCommoditySizeServiceImpl extends ServiceImpl<IntegralCommoditySizeMapper, IntegralCommoditySize> implements IIntegralCommoditySizeService {
    @Autowired
    private IntegralCommoditySizeMapper integralCommoditySizeMapper;
    @Resource
    private IntegralCommodityMapper integralCommodityMapper;
    @Resource
    private IntegralOrderMapper integralOrderMapper;
    @Resource
    private CommodityRepertoryMapper commodityRepertoryMapper;


    /**
     * 查询积分商品尺寸
     *
     * @param id 积分商品尺寸主键
     * @return 积分商品尺寸
     */
    @Override
    public IntegralCommoditySize selectIntegralCommoditySizeById(Long id) {
        return integralCommoditySizeMapper.selectIntegralCommoditySizeById(id);
    }

    /**
     * 查询积分商品尺寸列表
     *
     * @param integralCommoditySize 积分商品尺寸
     * @return 积分商品尺寸
     */
    @Override
    public List<IntegralCommoditySize> selectIntegralCommoditySizeList(IntegralCommoditySize integralCommoditySize) {
        return integralCommoditySizeMapper.selectIntegralCommoditySizeList(integralCommoditySize);
    }

    /**
     * 新增积分商品尺寸
     *
     * @param integralCommoditySize 积分商品尺寸
     * @return 结果
     */
    @Override
    public int insertIntegralCommoditySize(IntegralCommoditySize integralCommoditySize) {
        return integralCommoditySizeMapper.insertIntegralCommoditySize(integralCommoditySize);
    }

    /**
     * 修改积分商品尺寸
     *
     * @param integralCommoditySize 积分商品尺寸
     * @return 结果
     */
    @Override
    public int updateIntegralCommoditySize(IntegralCommoditySize integralCommoditySize) {
        return integralCommoditySizeMapper.updateIntegralCommoditySize(integralCommoditySize);
    }

    /**
     * 批量删除积分商品尺寸
     *
     * @param ids 需要删除的积分商品尺寸主键
     * @return 结果
     */
    @Override
    public int deleteIntegralCommoditySizeByIds(Long[] ids) {
        return integralCommoditySizeMapper.deleteIntegralCommoditySizeByIds(ids);
    }

    /**
     * 删除积分商品尺寸信息
     *
     * @param id 积分商品尺寸主键
     * @return 结果
     */
    @Override
    public int deleteIntegralCommoditySizeById(Long id) {
        return integralCommoditySizeMapper.deleteIntegralCommoditySizeById(id);
    }

    @Override
    public List<IntegralCommodityRepertory> integralCommodityRepertory(String name) {

        LambdaQueryWrapper<IntegralCommodity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(name)) {
            queryWrapper.like(IntegralCommodity::getCommodityName, name);
        }
        List<IntegralCommodity> integralCommodities = integralCommodityMapper.selectList(queryWrapper);

        List<IntegralCommodityRepertory> integralCommodityRepertoryList = new ArrayList<>();

        for (IntegralCommodity integralCommodity : integralCommodities) {
            IntegralCommodityRepertory integralCommodityRepertory = new IntegralCommodityRepertory();
            integralCommodityRepertory.setCommodityId(integralCommodity.getId());
            integralCommodityRepertory.setCommodityName(integralCommodity.getCommodityName());
            integralCommodityRepertory.setCreateDate(integralCommodity.getCreatedTime());
            //总库存
            integralCommodityRepertory.setTotalRepertory(integralCommoditySizeMapper.selectCountByCommodityId(integralCommodity.getId()));
            //总销量
            integralCommodityRepertory.setTotalSalesVolume(integralOrderMapper.selectSalesVolumeByCommodityId(integralCommodity.getId()));

            integralCommodityRepertoryList.add(integralCommodityRepertory);
        }

        return integralCommodityRepertoryList;
    }
}
